package pub.zhouhui.ch1.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Created by shane on 16-11-5.
 */

@Aspect//通过Aspect 注解声明一个切面
@Component//通过@Component让此切面成为Spring容器管理的Bean
public class LogAspect {
    @Pointcut("@annotation(pub.zhouhui.ch1.aop.Action)")    //通过@Pointcut 注解声明切点
    public void annotationPointCut(){

    };
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("注解式拦截" + action.name());
    }

    @Before("execution(* pub.zhouhui.ch1.aop.DemoMethodService.* (..))")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则式拦截," + method.getName());
    }

}
